﻿using B.S.BaseData.Api.Write.Applicantion.Command.GoodsCommand;
using B.S.BaseData.Api.Write.GetCelim;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interface;
using B.S.BataBase.Domain.Entitys;
using MediatR;
using Yitter.IdGenerator;

namespace B.S.BaseData.Api.Write.Applicantion.CommandHandler.GoodsCommandHandler
{
    public class GoodschargeInfoAddCommandHandler : IRequestHandler<GoodschargeInfoAddCommand, APIResult<int>>
    {
        private readonly IBaseRepository<GoodschargeInfo> _repository;
        private readonly IIdContextCemil _iidContextCemil;
        public GoodschargeInfoAddCommandHandler(IBaseRepository<GoodschargeInfo> repository, IIdContextCemil iidContextCemil)
        {
            _repository = repository;
            _iidContextCemil = iidContextCemil;
        }
        public async Task<APIResult<int>> Handle(GoodschargeInfoAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            try
            {
                GoodschargeInfo info = new GoodschargeInfo();
                info.Goodschargegroup = request.Goodschargegroup;
                info.GoodschargeHead = request.GoodschargeHead;
                info.Id = YitIdHelper.NextId();
                info.IsDeleted = false;
                info.CreateName = _iidContextCemil.UserName;
                info.GoodsId = request.GoodsId;

                result.Code = ApiEnum.Success;
                result.Msg = "深加工信息添加成功";
                result.Data = await _repository.CreateAsync(info);
                return await Task.FromResult(result);
            }
            catch (Exception)
            {
                result.Code = ApiEnum.Fail;
                result.Msg = "深加工信息添加失败";
                return await Task.FromResult(result);
                throw;
            }
        }
    }
}
